<template>
  <div>
    <el-container style="height: 800px; border: 1px solid #eee">
      <el-aside  width="200px" style="background-color: rgb(238, 241, 246)">
        <Aside></Aside>
      </el-aside>
      <el-container>
        <el-header style="text-align: right; font-size: 12px;">
          <Head></Head>
        </el-header>
        <el-main>
          <el-table
              :data="tableData"
              style="width: 100%">
            <el-table-column
                prop="studentId"
                label="学生学号"
                >
            </el-table-column>
            <el-table-column
                prop="studentName"
                label="学生姓名"
                >
            </el-table-column>
            <el-table-column
                prop="studentSex"
                label="性别">
            </el-table-column>
            <el-table-column
                prop="studentBirthday"
                label="出生日期">
            </el-table-column>
            <el-table-column
                prop="classId"
                label="班级ID">
            </el-table-column>
            <el-table-column
                fixed="right"
                label="操作"
                width="100px">
              <template #default="scope">
                <el-button @click="editInfo(scope.row, doMethod)" type="text" size="small">编辑</el-button>
                <el-button type="text" size="small" @click="confirmDelete(scope.row.studentId)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>

          <el-dialog title="编辑" v-model="dialogFormVisible">
            <el-form :model="form">
<!--              <el-form-item label="学生学号" :label-width="formLabelWidth">-->
<!--                <el-input v-model="form.studentId" autocomplete="off"></el-input>-->
<!--              </el-form-item>-->
              <el-form-item label="学生姓名" :label-width="formLabelWidth">
                <el-input v-model="form.studentName" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="性别" :label-width="formLabelWidth">
                <el-input v-model="form.studentSex" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="出生日期" :label-width="formLabelWidth">
                <el-input v-model="form.studentBirthday" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="班级ID" :label-width="formLabelWidth">
                <el-input v-model="form.classId" autocomplete="off"></el-input>
              </el-form-item>
            </el-form>
            <template #footer>
                    <span class="dialog-footer">
                      <el-button @click="dialogFormVisible = false">取 消</el-button>
                      <el-button type="primary" @click="pushEditInfo(doMethod)">确 定</el-button>
                    </span>
            </template>
          </el-dialog>

          <el-button class="el-button" size="medium" @click="addInfo">新增</el-button>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import Aside from "@/components/Aside";
import Head from "@/components/Head";

export default {
  name: "Student",
  components: {
    Aside,
    Head
  },
  data() {
    return {
      tableData : [],
      formLabelWidth : '120px',
      dialogFormVisible: false,
      doMethod : '',
      form: {
        studentId: '',
        studentName: '',
        studentSex: '',
        studentBirthday: '',
        classId: ''
      }
    }
  },
  created() {
    this.getStudentList()
  },
  methods: {
    getStudentList() {
      const _this = this
      _this.axios.get("/student/list").then(res => {
        console.log(res.data.data)
        this.tableData = res.data.data
      })
    },
    editInfo(row) {
      this.form.studentId = row.studentId
      this.form.studentName = row.studentName
      this.form.studentSex = row.studentSex
      this.form.studentBirthday = row.studentBirthday
      this.form.classId = row.classId
      this.dialogFormVisible = true
      this.doMethod = 'edit'
    },
    pushEditInfo(action) {
      const _this = this;
      this.axios.post("/student/" + action, this.form).then(res => {
        console.log(res)
        _this.dialogFormVisible = false
        _this.getStudentList()
      })
    },
    addInfo() {
      this.form.studentId = ''
      this.form.studentName = ''
      this.form.studentSex = ''
      this.form.studentBirthday = ''
      this.form.classId = ''
      this.dialogFormVisible = true
      this.doMethod = 'add'
    },
    confirmDelete(id) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const _this = this;
        this.axios.post("/student/del/" + id).then(res => {
          console.log(res)
          _this.getStudentList()
        })
        // this.$message({
        //   type: 'success',
        //   message: '删除成功!'
        // });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }
  }
}
</script>

<style scoped>

</style>
